<?php

use think\migration\Seeder;

class Permission extends Seeder
{
    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * http://docs.phinx.org/en/latest/seeding.html
     */
    public function run()
    {
        \think\Db::execute("truncate table user_permission");
        \think\Db::execute("truncate table role_permission");
        \think\Db::execute("truncate table permission");
        $comData = [
            'description' => '',
            'icon' => 'layui-icon-face-smile',
        ];
        $data = [
            [
                'name' => '系统管理',
                'type' => 2,
                'parent_id' => 0,
                'url' => '',
                'sort' => 0,
                'child' => [
                    [
                        'name' => '用户管理',
                        'type' => 1,
                        'url' => 'admin/user/index',
                        'sort' => 1,
                        'child' => [
                            [
                                'name' => '添加',
                                'type' => 1,
                                'url' => 'admin/user/add',
                                'sort' => 2,
                            ],
                            [
                                'name' => '编辑',
                                'type' => 1,
                                'url' => 'admin/user/edit',
                                'sort' => 3,
                            ],
                            [
                                'name' => '删除',
                                'type' => 1,
                                'url' => 'admin/user/del',
                                'sort' => 4,
                            ],
                            [
                                'name' => '分配角色',
                                'type' => 1,
                                'url' => 'admin/user/role',
                                'sort' => 5,
                            ],
                            [
                                'name' => '分配权限',
                                'type' => 1,
                                'url' => 'admin/user/permission',
                                'sort' => 6,
                            ],
                        ],
                    ],
                    [
                        'name' => '角色管理',
                        'type' => 1,
                        'url' => 'admin/role/index',
                        'sort' => 2,
                        'child' => [
                            [
                                'name' => '添加',
                                'type' => 1,
                                'url' => 'admin/role/add',
                                'sort' => 2,
                            ],
                            [
                                'name' => '编辑',
                                'type' => 1,
                                'url' => 'admin/role/edit',
                                'sort' => 3,
                            ],
                            [
                                'name' => '删除',
                                'type' => 1,
                                'url' => 'admin/role/del',
                                'sort' => 4,
                            ],
                            [
                                'name' => '分配权限',
                                'type' => 1,
                                'url' => 'admin/role/permission',
                                'sort' => 5,
                            ],
                        ],
                    ],
                    [
                        'name' => '权限管理',
                        'type' => 1,
                        'url' => 'admin/permission/index',
                        'sort' => 3,
                        'child' => [
                            [
                                'name' => '添加',
                                'type' => 1,
                                'url' => 'admin/permission/add',
                                'sort' => 2,
                            ],
                            [
                                'name' => '编辑',
                                'type' => 1,
                                'url' => 'admin/permission/edit',
                                'sort' => 3,
                            ],
                            [
                                'name' => '删除',
                                'type' => 1,
                                'url' => 'admin/permission/del',
                                'sort' => 4,
                            ],
                        ],
                    ],
                ],
            ],
        ];

        foreach ($data as $d1){
            $p1 = \app\admin\model\Permission::create(array_merge($d1,$comData));
            if (isset($d1['child']) && !empty($d1['child'])){
                foreach ($d1['child'] as $d2){
                    $p2 = \app\admin\model\Permission::create(array_merge($d2,$comData,['parent_id'=>$p1->id]));
                    if (isset($d2['child']) && !empty($d2['child'])){
                        foreach ($d2['child'] as $d3){
                            $p3 = \app\admin\model\Permission::create(array_merge($d3,$comData,['parent_id'=>$p2->id]));
                        }
                    }
                }
            }
        }
    }
}